// components/head-arr/head-arr.js
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        imgArrs: Array,
        indexPage: Boolean,
    },

    /**
     * 组件的初始数据
     */
    data: {
        imgArr: ['https://static.map8.com/sp/20200115/0_144331337686.jpg'],
    },
    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () {},
    },
    /**
     * 组件的方法列表
     */
    methods: {
        getImgInfo() {
            let _that = this;
            if (!_that.data.indexPage) {
                let imgArrItem = [];
                wx.request({
                    url: app.globalData.rootPath + `code/getHomePageCityInfo`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded', // 默认值
                    },
                    data: {
                        country: app.globalData.countryBack,
                        city: app.globalData.addressBack,
                    },
                    success: function (res) {
                        if (
                            !res.data.secretaryList ||
                            res.data.secretaryList.length == 0
                        ) {
                            imgArrItem.push(
                                'https://static.map8.com/sp/20200115/0_144331337686.jpg'
                            );
                        } else {
                            let imgUrl;
                            if (res.data.secretaryList.length < 3) {
                                for (
                                    let i = 0;
                                    i < res.data.secretaryList.length;
                                    i++
                                ) {
                                    imgUrl =
                                        res.data.secretaryList[i]
                                            .weixinpicurl ||
                                        res.data.secretaryList[i].realnapicurl;
                                    imgArrItem.push(imgUrl);
                                }
                            } else {
                                for (let i = 0; i < 3; i++) {
                                    imgUrl =
                                        res.data.secretaryList[i]
                                            .weixinpicurl ||
                                        res.data.secretaryList[i].realnapicurl;
                                    imgArrItem.push(imgUrl);
                                }
                            }
                        }
                        _that.setData({
                            imgArr: imgArrItem,
                        });
                        _that.triggerEvent('getHomePageInfo', res.data);
                    },
                });
            } else {
                _that.setData({
                    imgArr: _that.data.imgArrs,
                });
            }
        },
    },
});
